package com.ambimmort.shj.service.send.policy;

import cn.net.greenet.common.db_service.BaseDao;
import cn.net.greenet.common.ucagent_service.UcAgentSupport;
import cn.net.greenet.db.HibernateUtil;
import cn.net.greenet.db.bean.Dpiendpointbean;
import cn.net.greenet.send.policy.GetDeviceUntil;
import cn.net.greenet.send.policy.SendPolicyUntil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Josh on 2015/7/31.
 */
@Service
public class SendPolicyService {
//    @Autowired
//    private SessionFactory hibernateSessionFactory;
    ResultSet rs =null;
    BaseDao baseDao = null;

    private void init() {
        baseDao = new BaseDao();
    }

    private void close() {
        if (baseDao != null) {
            baseDao.close();
        }
    }

    public String getDeviceInfoService(String id) throws SQLException {
        return GetDeviceUntil.getDevice(id);

    }

    public boolean handSendPolicyQueue(String json) throws UnsupportedEncodingException {

        List<Map<String, String>> jsonData = new ArrayList<>();  //存放策略库信息
        System.out.println("receive json" + json);
        JSONArray jsonArr = JSONArray.fromObject(json);
        for (int i = 0; i < jsonArr.size(); i++) {
            Map<String, String> temporaryData = new HashMap<>();
            JSONObject jo = jsonArr.getJSONObject(i);
            temporaryData.put("type", jo.getString("type"));
            temporaryData.put("instance", new String(Base64.decodeBase64(jo.getString("instance")), "utf-8"));
            temporaryData.put("messageNo", jo.getString("messageNo"));
            temporaryData.put("policyRepositoryID", jo.getString("policyRepositoryID"));
            temporaryData.put("deviceID", jo.getString("deviceID"));
            jsonData.add(temporaryData);
        }
        boolean flag = SendPolicyUntil.writeMessageQueue(jsonData);
        return flag;
    }

    public String sendPolicyUcAgent(){
        init();
        Object[] params ={};
        rs=baseDao.executeSQL("select * from Dpiendpointbean",params);
        Dpiendpointbean bean = null;
        try {
            while (rs.next()){
                if (!bean.getMessageoutgoingsendingqueues().isEmpty()) {
                    UcAgentSupport.getInstance().getApi().msg_send_manaul(bean.getName());
                }
            }
            return "{\"result\":\"true\"}";
        } catch (SQLException e) {
            e.printStackTrace();
            return "{\"result\":\"false\"}";
        }
        finally {
            close();
        }

//        SessionFactory sf = HibernateUtil.getSessionFactory();
//        Session session = sf.openSession();
//        session.clear();
//        Query query = session.createQuery("FROM Dpiendpointbean");
//        List l = query.list();
//        Dpiendpointbean bean = null;
//        for (int i = 0; i < l.size(); i++) {
//            bean = (Dpiendpointbean) l.get(i);
//            if (!bean.getMessageoutgoingsendingqueues().isEmpty()) {
//
//                UcAgentSupport.getInstance().getApi().msg_send_manaul(bean.getName());
//
//            }
//        }
    }
}
